/**
 * Created by sky on 9/1/2017.
 */
let foodFacade = require('../facade/food.server.facade');
const uuid4 = require('uuid/v4');
const fileUtil = require('../utils/file.util');
const config = require('../config/env/config_development.json');
const multiParty = require('connect-multiparty');
const _ = require('lodash');


module.exports = {
    findAllFoods: findAllFoods,
    addFood:addFood,
    updateFood:updateFood,
    deleteFood:deleteFood,
    findFoodByCategories:findFoodByCategories
};

function findAllFoods(req, res, next) {
    console.log("food_controller  merchantId"+req.merchantId);
    let merchant = req.merchantId;
    foodFacade.findAllFoods(merchant,(error, result) => {
        if(error){
            return next(new Error(error));
        }else {
            _.forEach(result,function(item){
                item.image = config.file.apiPath+item.image;
            });
            return res.json(result);
        }
    })

//测试用
    //    foodFacade.findAllFoods((error, result) => {
    //     console.log("food controller");
    //     console.log(req.merchant.id);
    //     if(error){
    //         return next(new Error(error));
    //     }else {
    //         return res.json(result);
    //     }
    // })
}

function addFood(req, res, next) {
    console.log("add_food_controller");
    let body = req.body;
    let foodImage = req.files.file.image;
     body.merchant = req.merchantId;
    var destPath = "food/" + fileUtil.getName(foodImage);
    body.image = destPath;
    fileUtil.readFile(foodImage.path, function (error, data) {
        fileUtil.writeFile(config.file.path + destPath, data, function (error) {
            if (error) {
                return next(new Error(error))
            }
            foodFacade.addFood(body, (error, result) => {
                if (error) {
                    return next(new Error(error));
                } else {
                    return res.json(result);
                }
            });
        })
    })
}

function updateFood(req, res, next) {
    console.log("update_food_controller");
    let body = req.body;
    let foodImage = req.files.file.image;
    var destPath = "food/" + fileUtil.getName(foodImage);
    body.image = destPath;

    fileUtil.readFile(foodImage.path, function (error, data) {
        fileUtil.writeFile(config.file.path + destPath, data, function (error) {
            if (error) {
                return next(new Error(error))
            }
            foodFacade.updateFood(body,(error, result) => {
                if(error){
                    return next(new Error(error));
                }else {
                    return res.json(result);
                }
            });
        })
    })







};

function deleteFood(req, res, next) {
    console.log("delete_food_controller");
    let params = req.params;
    let id = params.id;
    foodFacade.deleteFood(id,(error, result) => {
        if(error){
            return next(new Error(error));
        }else {
            return res.json(result);
        }
    });
};


function findFoodByCategories(req, res, next) {
    console.log("food_categories_controller");
    let merchantId = req.merchantId; 
    let params = req.params;
    let categories = params.categories;
    if(categories != "ALL"){
        foodFacade.findFoodByCategories(merchantId,categories,(error, result) => {
            if(error){
                return next(new Error(error));
            }else {
                _.forEach(result,function(item){
                    item.image = config.file.apiPath+item.image;
                });
                return res.json(result);
            }
        });

        //测试用
        // foodFacade.findFoodByCategories(categories,(error, result) => {
        //     console.log("catcat"+JSON.stringify(categories))
        //     if(error){
        //         return next(new Error(error));
        //     }else {
        //         return res.json(result);
        //     }
        // });
    }
    else{
        foodFacade.findAllFoods(merchantId,(error, result) => {
        if(error){
            return next(new Error(error));
        }else {
            _.forEach(result,function(item){
                item.image = config.file.apiPath+item.image;
            });
            return res.json(result);
        }
    })

//测试用
    //    foodFacade.findAllFoods((error, result) => {
    //     if(error){
    //         return next(new Error(error));
    //     }else {
    //         return res.json(result);
    //     }
    // })
    }
};
